 import validate from 'wechat-validate'
Page({
  behaviors: [validate],
  rules: {
    mobile: [
      { required: true, message: '请输入手机号' },
      { pattern: /^1[3-8]\d{9}$/, message:'请输入正确手机号'}
    ],
    code: [
      { required: true, message: '请输入验证码' },
      { pattern: /^\d{6}$/, message:'请输入正确的验证码'}
    ],

  },
  data: {
    countDownVisible: false,
    mobile: '',
    code: '',
  },
  onLoad({redirectUrl,routeType}) { 
    this.redirectUrl = redirectUrl
    this.routeType = routeType
    console.log(this.routeType);
    
  },
  

  countDownChange(ev) {
    this.setData({
      timeData: ev.detail,
      countDownVisible: ev.detail.minutes === 1 || ev.detail.seconds > 0,
    })
  },
  async getMsg() {
    const { valid, message } = this.validate('mobile')
    if(!valid)  return wx.utils.toast(message)
    
    this.setData({ countDownVisible: true })
    // 发送验证码
    const { code, data } = await wx.http.get('/code', { mobile: this.data.mobile })
    if (code !== 10000) return wx.utils.toast()
  },
  async goLogin() {
    if(!this.validate()) return
    const { code, data } = await wx.http.post('/login', { mobile: this.data.mobile, code: this.data.code })
    if (code !== 10000) return wx.utils.toast('登录失败,请重新登录!')
    // 存储token
    getApp().setToken('token', data.token)
    getApp().setToken('refreshToken', data.refreshToken)
    // 跳转新页面
    wx[this.routeType]({
      url: this.redirectUrl,
    })
  }
})
